/**
 * @description 封装axios数据请求
 * @author zhangli
 */
import axios from "axios";
import qs from "qs";

// 请求超时配置
axios.defaults.timeout = 40000;
// 跨域接口请求自动带上cookie信息
axios.defaults.withCredentials = true;
// 拦截请求
axios.interceptors.request.use(
    function (config) {
        config.headers = {}
        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);

// 拦截响应
axios.interceptors.response.use(
    function (response) {
        return response;
    },
    function (error) {
        return Promise.reject(error);
    }
);

// 请求加载效果
const showToast = () => { };
// 隐藏加载效果
const hideToast = () => { };

// 处理响应结果
function responseRes(res, isTip) {
    hideToast();
    return new Promise((resolve, reject) => {
        if (
            res.code == 0 ||
            res.code == 200 ||
            res.status == 200
        ) {
            resolve(res);
        } else if (res.code === 401) {
            reject(res);
        } else {
            // 是否有错误提示, 根据产品ui效果进行提示
            if (isTip) {
            }
            reject();
        }
    });
}

const $http = {
    async get(obj, loading = true, isTip = true) {
        if (loading) showToast();
        try {
            let res = await axios.get(obj.url, {
                params: obj.params || "",
            });
            res = res.data;
            return responseRes(res, isTip);
        } catch (err) {
            return responseRes(err, isTip);
        }
    },
    async getUrl(obj, loading = true, isTip = true) {
        if (loading) showToast();
        try {
            let res = await axios.get(obj.url, {});
            res = res.data;
            return responseRes(res, isTip);
        } catch (err) {
            return responseRes(err, isTip);
        }
    },
    async post(obj, loading = true, isTip = true) {
        if (loading) showToast();
        try {
            let res = await axios({
                url: obj.url,
                method: "post",
                data: obj.params,
                headers: {
                    "Content-Type": "application/json",
                },
            });
            res = res.data;
            return responseRes(res, isTip);
        } catch (err) {
            return responseRes(err, isTip);
        }
    },
    async postForm(obj, loading = true, isTip = true) {
        if (loading) showToast();
        try {
            let res = await axios.post(obj.url, qs.stringify(obj.params));
            res = res.data;
            return responseRes(res, isTip);
        } catch (err) {
            return responseRes(err, isTip);
        }
    },
    async postJsonQuery(obj, loading = true, isTip = true) {
        if (loading) showToast();
        let url = obj.url + "?";
        for (const key in obj.params) {
            url += key + "=" + obj.params[key] + "&";
        }
        try {
            let res = await axios({
                url: url,
                method: "post",
                data: obj.params,
                headers: {
                    "Content-Type": "application/json",
                },
            });
            res = res.data;
            return responseRes(res, isTip);
        } catch (err) {
            return responseRes(err, isTip);
        }
    },
    async postQuery(obj, loading = true, isTip = true) {
        if (loading) showToast();
        let url = obj.url + "?";
        for (const key in obj.params) {
            url += key + "=" + obj.params[key] + "&";
        }
        try {
            let res = await axios.post(url, qs.stringify({}));
            res = res.data;
            return responseRes(res, isTip);
        } catch (err) {
            return responseRes(err, isTip);
        }
    },
};

export default $http;
